#include <libavformat/avformat.h>

#define MAX_VIDEO_FRAME_SIZE 32976
#define RTP_SIZE 980
int main(int argc, char const *argv[])
{
    int ret;
    AVIOContext *server, *client;
    AVDictionary *in_opts = NULL;
    // av_dict_set(&in_opts, "timeout", "2", 0);
    av_dict_set(&in_opts, "listen", "2", 0);

    ret = avio_open2(&server, "tcp://0.0.0.0:9808", AVIO_FLAG_READ, NULL, &in_opts);

    while (1)
    {
        avio_accept(server, &client);

        char buf[RTP_BODY_SIZE];
        char video_buf[MAX_VIDEO_FRAME_SIZE];
        // char *buf = malloc(rtp_size);
        //  max frame less then 32k
        // char *video_buf = malloc(MAX_VIDEO_FRAME_SIZE);

        while (1)
        {
            ret = avio_read(client, buf, RTP_BODY_SIZE);
            if (ret < 0)
            {
                break;
            }

            // log start
            for (size_t i = 0; i < RTP_BODY_SIZE; i++)
            {
                printf("%#X ", buf[i]);
            }
            printf("\n\n\n");
            // log end

            char av_type_pkt_flag = buf[15];

            if (av_type_pkt_flag & 0xf0 != 0x30)
            {
                // video packet, put to tmp_buf
                if (av_type_pkt_flag & 0x0f != 0x02)
                {
                    printf("video frame\n");
                }
                else
                {
                    // last packet of video
                    memset(video_buf, 0, MAX_VIDEO_FRAME_SIZE);
                }
            }

            // clear buf
            memset(buf, 0, RTP_BODY_SIZE);
        }
    }

    return 0;
}
